package control;
import java.util.ArrayList;
import java.util.Arrays;


/**
 * SortWordsApp
 * manipulador de palavras.
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 */
public class SortWordsApp {
	 public String texto; 


	/**
	 * Construtor SortWordsApp 
	 *  Clean up punctuation
	 *  Remove multiple empty spaces
	 *  order alphabetically
	 *  clean array of common words
	 *  count word freq
	 *  order words by freq
	 * @param line line
	 */
	public SortWordsApp(String line) {

		System.out.println("Inicio do SortWordsApp   ");	

		line = line.replaceAll("[.,;:!?()\r\n]", " "); // Clean up punctuation
		line = line.replaceAll(" +", " "); // Remove multiple empty spaces
		line = line.toLowerCase();

		String[] textArray = line.split(" ");
		java.util.Arrays.sort(textArray); 

		textArray = removeCommonWords(textArray); // clean array of common words
		String[] freq_WordArray = wdsInFreqOrder(textArray); // count word freq
		freq_WordArray = reverseArray(freq_WordArray); // order words by freq

		// Output list of pairs: (n, word)
		line = Arrays.toString(freq_WordArray);
		line = line.replace('[', ' '); // remove [,]
		line = line.replace(']', ' ');
		line = line.replaceAll(",", "\n");
		this.texto = line;
		// line=line.replaceAll("[[]]"," ");

		System.out.println("Fim do SortWordsApp   ");		
		//return;
	}



	/**
	 * PadDigits
	 * left pad digit strings with zeroes or anything else
	 * @param c c
	 * @param padLength padLength
	 * @param digits digits
	 * @return s
	 */
	public String padDigits(String c, int padLength, int digits) {		
		System.out.println("Inicio do SortWordsApp -> padDigits  ");

		String s = Integer.toString(digits);
		int nDigits = s.length();
		int nPads = padLength - nDigits;		
//		if (nPads < 0) ; // error		
		if (nPads >= 0) {
			for (int i = nPads; i > 0; i--) {
				s = c.concat(s);
			}	
		}		
		System.out.println("Fim do SortWordsApp -> padDigits  ");
		return s;
	}

	/**
	 * wdsInFreqOrder
	 * metodo que ordena um array de strings por frequencia.
	 * @param array array
	 * @return freqWord
	 */
	public String[] wdsInFreqOrder(String[] array) {
		System.out.println("Inicio do SortWordsApp -> wdsInFreqOrder  ");

		int total_words = array.length;
		int count = 1;
		ArrayList<String> list_word = new ArrayList<String>();
		for (int i = 1; i < total_words; i++) {
			if (array[i - 1].equals(array[i])) {
				count++;
			} else {
				// zero pad the count string
				String s = padDigits("0", 4, count);
				s = s.concat("\t");
				list_word.add(s.concat(array[i - 1]));// combine freq with word
				count = 1;
			}
		}

		// Incluir o ultimo elemento do array na lista list_word
		String s = padDigits("0", 4, count);
		s = s.concat("\t");
		list_word.add(s.concat(array[total_words - 1]));

		// Convert ArrayList to an array of Strings
		String[] freqWord = list_word.toArray(new String[0]);
		java.util.Arrays.sort(freqWord);

		System.out.println("Fim do SortWordsApp -> wdsInFreqOrder  ");
		return freqWord;
	}


	/**
	 * removeCommonWords
	 * remove as palavras comuns
	 * @param array array
	 * @return cleanArray
	 */
	public String[] removeCommonWords(String[] array) {
		System.out.println("Inicio do SortWordsApp -> removeCommonWords  ");

		String[] removeArray = new String[] { "0", "1", "2", "3", "4", "5",
				"6", "7", "8", "9", "10", "a", "an", "and", "are", "as", "at",
				"be", "by", "can", "each", "every", "first", "for", "from",
				"had", "has", "have", "if", "in", "is", "it", "n", "not", "m",
				"may", "mu", "of", "on", "one", "only", "other", "or", "said",
				"second", "show", "so", "such", "than", "that", "the", "then",
				"there", "these", "this", "thus", "to", "up", "very", "was",
				"we", "what", "when", "which", "who", "will", "with", "would" };
		Arrays.sort(removeArray); // binarySearch needs removeArray sorted
		ArrayList<String> cleanList = new ArrayList<String>();
		for (int i = 0; i < array.length; i++) {
			int loc = Arrays.binarySearch(removeArray, array[i]);
			if (loc < 0) {				
				cleanList.add(array[i]);
			}
		}
		String[] cleanArray = cleanList.toArray(new String[0]);

		System.out.println("Fim do SortWordsApp -> removeCommonWords  ");
		return cleanArray;
	}

	/**
	 * reverseArray
	 * inverte a ordem do array
	 * @param array array
	 * @return arrayRev arrayRev
	 */
	public String[] reverseArray(String[] array) {
		System.out.println("Inicio do SortWordsApp -> reverseArray  ");
		int n = array.length;
		String[] arrayRev = new String[n];
		for (int i = 0; i < n; i++){
			arrayRev[n - i - 1] = array[i];
		}
		System.out.println("Fim do SortWordsApp -> reverseArray  ");
		return arrayRev;
	}




	/**
	 * getTexto
	 * @return texto
	 */
	public String getTexto() {
		return texto;
	}

	/**
	 * setTexto
	 * @param texto texto
	 */
	public void setTexto(String texto) {
		this.texto = texto;
	}

}
